package com.example.armmvvm

import kotlinx.coroutines.*
import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.system.measureTimeMillis

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    private var sDouble = 1
    private val doubleInt: Int
        get() {
            return 100 * sDouble
        }

    @Test
    fun testIncrement() {
        println(doubleInt)
        sDouble = 2
        println(doubleInt)
        sDouble = 3
        println(doubleInt)
    }

    @Test
    fun testChar() {
        println(getDataList(40))
    }

    private fun getDataList(itemCount: Int): MutableList<String> {
        return (0..itemCount).map {
            Char(it % 26 + 65).toString()
        }.toMutableList()
    }

    /**
     * 协程:测试两个请求同时执行
     */
    @Test
    fun testCor() {
        GlobalScope.launch {
            /*measureTimeMillis返回给定的block代码的执行时间*/
            val time = measureTimeMillis {
                val one = async {
                    one()
                }
                val two = async {
                    two()
                }
                val intValue = one.await()
                val strValue = two.await()
                println(intValue.toString())
                println(strValue)
                val three = one.await()
                println(three)

                println("两个方法返回值的和")
            }
            println("执行耗时：${time}")
        }
        println("----------------")
        /*应为上面的协程代码并不会阻塞掉线程，所以我们这里让线程睡4秒，保证线程的存活，在实际的Android开发中无需这么做*/
        Thread.sleep(4000)
    }

    suspend fun one(): Int {
        delay(1500)
        return 1
    }

    suspend fun two(): String {
        delay(2000)
        return "test size"
    }
}
